﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Public Class Ingresar_Paciente

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim adp6 As New SqlCommand("Buscar_Paciente", Sql.coner1)
        adp6.CommandType = CommandType.StoredProcedure
        adp6.Parameters.AddWithValue("@Rut", TxtRut.Text)
        adp6.Parameters.AddWithValue("@Validar_Rut", TxtValidar.Text)
        Dim ds6 As New DataSet()
        Dim dt6 As DataTable = New DataTable()
        Dim da6 As New SqlDataAdapter(adp6)
        da6.Fill(dt6)
        If dt6.Rows.Count > 0 Then
            MsgBox("El Paciente Ya esta registrado", MsgBoxStyle.Information, "Registro")
            Return
        Else

            Sql.coner1.Close()
            Sql.coner1.Open()
            Dim Ingresar1 As New SqlCommand("Ingresar_Paciente", Sql.coner1)
            Ingresar1.CommandType = CommandType.StoredProcedure
            Ingresar1.Parameters.AddWithValue("@Rut", TxtRut.Text)
            Ingresar1.Parameters.AddWithValue("@Validar_Rut", TxtValidar.Text)
            Ingresar1.Parameters.AddWithValue("@Nombre", TxtNombres.Text)
            Ingresar1.Parameters.AddWithValue("@Apellido", TxtApellidos.Text)
            Ingresar1.Parameters.AddWithValue("@Direccion", TxtDireccion.Text)
            Ingresar1.Parameters.AddWithValue("@Sexo", Ingresar.Sexo)
            Ingresar1.Parameters.AddWithValue("@Edad", TxtEdad.Text)
            Ingresar1.Parameters.AddWithValue("@UnidadFuncional", Ingresar.Funcion)
            Ingresar1.Parameters.AddWithValue("@Cama", Ingresar.Cama)
            Ingresar1.Parameters.AddWithValue("@Forma_Pago", ComboBox3.Text)
            Ingresar1.Parameters.AddWithValue("@FechaIngreso", Form1.ToolStripStatusLabel1.Text)
            Ingresar1.Parameters.AddWithValue("@Contra", RichTextBox3.Text)
            Ingresar1.Parameters.AddWithValue("@FechaFinal", " ")

            Sql.coner1.Close()
            Sql.coner1.Open()
            Ingresar1.ExecuteNonQuery()
            Ingresar1.Dispose()
            Sql.coner1.Close()

            MsgBox("El Paciente fue ingresado correctamente", MsgBoxStyle.Information, "Registro")

        End If
    End Sub

    Private Sub Ingresar_Paciente_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TxtSexo.Visible = False
        ComboBox1.Visible = False
        ComboBox2.Visible = False
        ComboBox3.Visible = False
        TxtNombres.ReadOnly = True
        TxtApellidos.ReadOnly = True
        TxtDireccion.ReadOnly = True
        TxtEdad.ReadOnly = True
        RichTextBox3.ReadOnly = True
        TxtSexo1.Visible = True
        TxtFuncional.Visible = True
        TxtCama.Visible = True
        TxtPago.Visible = True
        TxtSexo1.ReadOnly = True
        TxtFuncional.ReadOnly = True
        TxtCama.ReadOnly = True
        TxtPago.ReadOnly = True


        Dim adp3 As New SqlCommand("Mostrar_Sexo", Sql.coner1)
        adp3.CommandType = CommandType.StoredProcedure
        Dim ds3 As New DataSet()
        Dim dt As DataTable = New DataTable()
        Dim da As New SqlDataAdapter(adp3)
        da.Fill(dt)
        If dt.Rows.Count > 0 Then
            TxtSexo.DataSource = dt
            TxtSexo.DisplayMember = dt.Columns(1).Caption.ToString
        End If
        Sql.coner1.Close()

        Dim adp2 As New SqlCommand("Mostrar_Funcion", Sql.coner1)
        adp2.CommandType = CommandType.StoredProcedure
        Dim ds2 As New DataSet()
        Dim dt2 As DataTable = New DataTable()
        Dim da2 As New SqlDataAdapter(adp2)
        da2.Fill(dt2)
        If dt2.Rows.Count > 0 Then
            ComboBox1.DataSource = dt2
            ComboBox1.DisplayMember = dt2.Columns(1).Caption.ToString
        End If
        Sql.coner1.Close()

        Dim adp1 As New SqlCommand("Mostrar_Cama", Sql.coner1)
        adp1.CommandType = CommandType.StoredProcedure
        adp1.Parameters.AddWithValue("@Funcion", Ingresar.Funcion)
        Dim ds1 As New DataSet()
        Dim dt1 As DataTable = New DataTable()
        Dim da1 As New SqlDataAdapter(adp1)
        da1.Fill(dt1)
        If dt1.Rows.Count > 0 Then
            ComboBox2.DataSource = dt1
            ComboBox2.DisplayMember = dt1.Columns(1).Caption.ToString
        End If
        Sql.coner1.Close()

    End Sub

    Private Sub BntBuscar_Click(sender As Object, e As EventArgs) Handles BntBuscar.Click
        If TxtRut.Text = "" Then
            MsgBox("Llene el campo rut de la busqueda", MsgBoxStyle.Information, "Error")
        Else
            Sql.coner1.Close()
            Sql.coner1.Open()
            Dim adp6 As New SqlCommand("Buscar_Paciente1", Sql.coner1)
            adp6.CommandType = CommandType.StoredProcedure
            adp6.Parameters.AddWithValue("@Rut", TxtRut.Text)
            adp6.Parameters.AddWithValue("@Validar_Rut", TxtValidar.Text)
            adp6.Parameters.AddWithValue("@Fecha", Form1.ToolStripStatusLabel1.Text)
            adp6.Parameters.AddWithValue("@Fecha_Termino", " ")
            Dim ds6 As New DataSet()
            Dim dt6 As DataTable = New DataTable()
            Dim da6 As New SqlDataAdapter(adp6)
            da6.Fill(dt6)
            If dt6.Rows.Count > 0 Then

                MsgBox("El paciente ya esta registrado el dia de hoy " & Form1.ToolStripStatusLabel1.Text & "", MsgBoxStyle.Information, "Registro")

            Else
                Sql.coner1.Close()
                Sql.coner1.Open()
                Dim adp1 As New SqlCommand("Buscar_Paciente", Sql.coner1)
                adp1.CommandType = CommandType.StoredProcedure
                adp1.Parameters.AddWithValue("@Rut", TxtRut.Text)
                adp1.Parameters.AddWithValue("@Validar_Rut", TxtValidar.Text)
                Dim ds1 As New DataSet()
                Dim dt1 As DataTable = New DataTable()
                Dim da1 As New SqlDataAdapter(adp1)
                da1.Fill(dt1)
                If dt1.Rows.Count > 0 Then

                    TxtNombres.Text = dt1.Rows(0).Item(0)
                    TxtApellidos.Text = dt1.Rows(0).Item(1)
                    TxtDireccion.Text = dt1.Rows(0).Item(2)
                    TxtSexo1.Text = dt1.Rows(0).Item(3)
                    TxtEdad.Text = dt1.Rows(0).Item(4)

                    TxtSexo.Visible = True
                    ComboBox1.Visible = True
                    ComboBox2.Visible = True
                    ComboBox3.Visible = True
                    TxtNombres.ReadOnly = True
                    TxtApellidos.ReadOnly = True
                    TxtDireccion.ReadOnly = False
                    TxtEdad.ReadOnly = False
                    RichTextBox3.ReadOnly = False
                    TxtSexo1.Visible = False
                    TxtFuncional.Visible = False
                    TxtCama.Visible = False
                    TxtPago.Visible = False


                Else
                    MsgBox("El Paciente no esta registrado", MsgBoxStyle.Information, "Registro")
                    TxtSexo.Visible = True
                    ComboBox1.Visible = True
                    ComboBox2.Visible = True
                    ComboBox3.Visible = True
                    TxtNombres.ReadOnly = False
                    TxtApellidos.ReadOnly = False
                    TxtDireccion.ReadOnly = False
                    TxtEdad.ReadOnly = False
                    RichTextBox3.ReadOnly = False
                    TxtSexo1.Visible = False
                    TxtFuncional.Visible = False
                    TxtCama.Visible = False
                    TxtPago.Visible = False

                End If
            End If
        End If
    End Sub

    Private Sub TxtSexo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TxtSexo.SelectedIndexChanged

        Ingresar.Sexo = 0
        Sql.coner1.Close()
        Sql.coner1.Open()
        Dim adp1 As New SqlCommand("Buscar_Sexo", Sql.coner1)
        adp1.CommandType = CommandType.StoredProcedure
        adp1.Parameters.AddWithValue("@NombreSexo", TxtSexo.Text)
        Dim ds1 As New DataSet()
        Dim dt1 As DataTable = New DataTable()
        Dim da1 As New SqlDataAdapter(adp1)
        da1.Fill(dt1)
        If dt1.Rows.Count > 0 Then
            Ingresar.Sexo = dt1.Rows(0).Item(0)
        End If
        Sql.coner1.Close()

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Ingresar.Funcion = 0
        Sql.coner1.Close()
        Sql.coner1.Open()
        Dim adp1 As New SqlCommand("Buscar_Funcion", Sql.coner1)
        adp1.CommandType = CommandType.StoredProcedure
        adp1.Parameters.AddWithValue("@NombreFuncion", ComboBox1.Text)
        Dim ds1 As New DataSet()
        Dim dt1 As DataTable = New DataTable()
        Dim da1 As New SqlDataAdapter(adp1)
        da1.Fill(dt1)
        If dt1.Rows.Count > 0 Then
            Ingresar.Funcion = dt1.Rows(0).Item(0)
        End If
        Sql.coner1.Close()

        ComboBox2.Text = ""

        Dim adp2 As New SqlCommand("Mostrar_Cama", Sql.coner1)
        adp2.CommandType = CommandType.StoredProcedure
        adp2.Parameters.AddWithValue("@Funcion", Ingresar.Funcion)
        Dim ds2 As New DataSet()
        Dim dt2 As DataTable = New DataTable()
        Dim da2 As New SqlDataAdapter(adp2)
        da2.Fill(dt2)
        If dt2.Rows.Count > 0 Then
            ComboBox2.DataSource = dt2
            ComboBox2.DisplayMember = dt2.Columns(1).Caption.ToString
        End If
        'Sql.coner1.Close()

    End Sub

    Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
        Ingresar.Cama = 0
        Sql.coner1.Close()
        Sql.coner1.Open()
        Dim adp1 As New SqlCommand("Buscar_Cama", Sql.coner1)
        adp1.CommandType = CommandType.StoredProcedure
        adp1.Parameters.AddWithValue("@NombreCama", ComboBox2.Text)
        adp1.Parameters.AddWithValue("@NombreFuncion", Ingresar.Funcion)
        Dim ds1 As New DataSet()
        Dim dt1 As DataTable = New DataTable()
        Dim da1 As New SqlDataAdapter(adp1)
        da1.Fill(dt1)
        If dt1.Rows.Count > 0 Then
            Ingresar.Cama = dt1.Rows(0).Item(0)
        End If
        Sql.coner1.Close()
       

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
End Class